package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.dubbo.mapper.UmsResourceCategoryMapper;
import com.mall.dubbo.mapper.UmsResourceMapper;
import com.mall.model.domain.UmsResource;
import com.mall.model.domain.UmsResourceCategory;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author xyiaao
 * @description 针对表【ums_resource_category(资源分类表)】的数据库操作Api实现
 * @createDate 2022-03-29 19:46:09
 */
@DubboService
public class UmsResourceCategoryApiImpl extends ServiceImpl<UmsResourceCategoryMapper, UmsResourceCategory>
        implements UmsResourceCategoryApi {

    @Autowired
    private UmsResourceMapper umsResourceMapper;

    @Autowired
    private UmsResourceCategoryMapper umsResourceCategoryMapper;

    @Override
    public boolean delete(Integer id) {

        // 先查询是否有资源关联了该分类
        LambdaQueryWrapper<UmsResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsResource::getCategoryId, id);
        if (umsResourceMapper.selectCount(queryWrapper) > 0) {
            return false;
        }
        umsResourceCategoryMapper.deleteById(id);
        return true;
    }

    @Override
    public List<UmsResourceCategory> listAll() {

        LambdaQueryWrapper<UmsResourceCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(UmsResourceCategory::getSort);
        return umsResourceCategoryMapper.selectList(queryWrapper);
    }

}




